package cn.com.yang.net;

import java.util.HashMap;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;

public class RestTemplateDemo {
	
	public static void main(String[] args) {
		RestTemplate restTemplate =new RestTemplate();
		String urlString ="https://xxxx.xxxx.xxx/xxxx/xxx/xxxxx.do";
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		HashMap<String, Object> params = new HashMap<String, Object>();
		HashMap<String, Object> headMap = new HashMap<String, Object>();
		HashMap<String, Object> bodyMap = new HashMap<String, Object>();
		headMap.put("H_CHNL_ID", "1005");
		headMap.put("H_NONCE", "2cb0bd30-8dfe-4c60-ab52-f80f0f3b6d9d");
		headMap.put("H_TIME", "1640157771345");
		headMap.put("H_TIME_OFFSET", "0");
		bodyMap.put("APP_VERSION","1.0.0");
		bodyMap.put("CHANNLEID", "gh_53f59e8b15d7");
		bodyMap.put("CHNL_ENCRYPT_TYPE", "0");
		bodyMap.put("CHNL_TYPE", "WB");
		params.put("head", headMap);
		params.put("body", bodyMap);
		
		String reqString = JSONObject.toJSONString(params);
		//HttpEntity entity = new HttpEntity(reqString, headers);
		HttpEntity<String> entity = new HttpEntity(reqString, headers);
		ResponseEntity<String> resultEntity =null;
		try {
			resultEntity = restTemplate.exchange(urlString, HttpMethod.POST,entity,String.class);
			System.out.println(resultEntity.toString());
			JSONObject  dataJsonObject = JSONObject.parseObject(resultEntity.getBody()).getJSONObject("head");
			System.out.println(dataJsonObject.getString("H_NONCE"));;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}
